using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Episode02
{
    public class Projectile : MonoBehaviour
    {
        public LayerMask collisionMask;
        float speed = 10;

        public void SetSpeed(float muzzleVelocity)
        {
            speed = muzzleVelocity;    
        }

        // Update is called once per frame
        void Update()
        {
            float moveDistance = Time.deltaTime * speed;
            CheckCollisions(moveDistance);
            transform.Translate(Vector3.forward * moveDistance);
        }

        void CheckCollisions(float moveDistance)
        {
            Ray ray = new Ray(transform.position, transform.forward);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, moveDistance, collisionMask, QueryTriggerInteraction.Collide))
            {
                OnHitObject(hit);
            }
        }

        void OnHitObject(RaycastHit hit)
        {
            print(hit.collider.gameObject.name);
            GameObject.Destroy(gameObject);
        }
    }
}